﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    ///  审核。特殊报告
    /// </summary>
    /// 创建时间：2022-12-22 11:37:15。作者:王浩力
    public partial class WindowTeshuGasCheck : Window
    {
        public WindowTeshuGasCheck()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 刷新父页面，列表分页
        /// </summary>
        public event EventHandler RefreshPageList;

        /// <summary>
        /// 被审核的记录
        /// </summary>
        internal Teshu_gas TeshuGas { get; set; }

        TeshuBusiness teshuBusiness = new TeshuBusiness();
        SourceDictBusiness sourceDictBusiness = new SourceDictBusiness();
        UserBusiness userBusiness = new UserBusiness();


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ////禁用关闭按钮
            //int handle = new WindowInteropHelper(this).Handle.ToInt32();
            //WinodowCloseButtonSet.Disable(handle);

            var list = sourceDictBusiness.GetAll();
            list = list.FindAll(g => g.D_stat == 1);

            //创建时间：2023-1-8 23:48:51
            var userList = userBusiness.GetAllUser();
            userList = userList.FindAll(g => g.U_stat == 1);

            //委托单位 cmb_client
            cmb_client.ItemsSource = list.FindAll(x => x.Group_id.Equals("quyang"));
            cmb_client.DisplayMemberPath = "Item_name";
            cmb_client.SelectedValuePath = "Id";

            //取样单位 cmb_sampling_unit
            cmb_sampling_unit.ItemsSource = list.FindAll(x => x.Group_id.Equals("quyang"));
            cmb_sampling_unit.DisplayMemberPath = "Item_name";
            cmb_sampling_unit.SelectedValuePath = "Id";

            //顶界层位 cmb_top_boundary
            cmb_top_boundary.ItemsSource = list.FindAll(g => g.Group_id.Equals("cengwei"));
            cmb_top_boundary.DisplayMemberPath = "Item_name";
            cmb_top_boundary.SelectedValuePath = "Id";

            //取样人 cmb_sampler
            cmb_sampler.ItemsSource = userList;
            cmb_sampler.DisplayMemberPath = "User_real_name";
            cmb_sampler.SelectedValuePath = "Id";

            //底界层位 cmb_bottom_boundary
            cmb_bottom_boundary.ItemsSource = list.FindAll(g => g.Group_id.Equals("cengwei"));
            cmb_bottom_boundary.DisplayMemberPath = "Item_name";
            cmb_bottom_boundary.SelectedValuePath = "Id";

            //取样位置  cmb_sampling_location
            cmb_sampling_location.ItemsSource = list.FindAll(g => g.Group_id.Equals("quyangLocation"));
            cmb_sampling_location.DisplayMemberPath = "Item_name";
            cmb_sampling_location.SelectedValuePath = "Id";

            //分析人   cmb_analyst
            cmb_analyst.ItemsSource = userList;
            cmb_analyst.DisplayMemberPath = "User_real_name";
            cmb_analyst.SelectedValuePath = "Id";

            //采样时间 
            date_sampling_time.Text = TeshuGas.Sampling_time;

            //编辑赋值
            this.txt_report_no.Text = TeshuGas.Report_no;
            this.txt_well_no.Text = TeshuGas.Well_no;
            this.txt_well_deep.Text = TeshuGas.Well_deep.ToString();

            this.txtSample_temperature.Text = TeshuGas.Sample_temperature.ToString();
            this.txtOil_pressure.Text = TeshuGas.Oil_pressure.ToString();
            this.cmb_client.SelectedValue = TeshuGas.Client;
            this.cmb_sampling_unit.SelectedValue = TeshuGas.Sampling_unit;

            this.cmb_top_boundary.SelectedValue = TeshuGas.Top_boundary;
            this.cmb_bottom_boundary.SelectedValue = TeshuGas.Bottom_boundary;
            this.txt_casing_pressure.Text = TeshuGas.Casing_pressure.ToString();

            this.cmb_sampler.SelectedValue = TeshuGas.Sampler;//取样人

            //this.cmb_sampling_location.SelectedValue = TeshuGas.Sampling_location;
            //取样位置；修改时间：2023-1-15 18:31:25
            if (Regex.IsMatch(TeshuGas.Sampling_location, "^\\d{1,19}"))
            {
                this.cmb_sampling_location.SelectedValue = TeshuGas.Sampling_location;
            }
            else
            {
                this.txt_sampling_location2.Text = TeshuGas.Sampling_location;
            }

            this.cmb_analyst.SelectedValue = TeshuGas.Analyst;

            //查询子项列表 ，添加时间：2022-12-21 16:41:55
            var itemList = teshuBusiness.GetTeshu_gas_itemList(TeshuGas.Id);
            this.listView.ItemsSource = itemList;

            txtReason.GotFocus += (s, e) =>
            {
                txtReason.Text = "";
            };

            //设置所有输入控件只读
            #region 设置所有输入控件只读
            var grid = (DependencyObject)this.Content;
            List<DependencyObject> list2 = new List<DependencyObject>();
            Action<DependencyObject> action = null;
            action = (a) =>
            {
                var count = VisualTreeHelper.GetChildrenCount(a);
                for (int i = 0; i < count; i++)
                {
                    var elem = VisualTreeHelper.GetChild(a, i);
                    list2.Add(elem);
                    action(elem);
                }
            };

            action(grid);

            foreach (var item in list2)
            {
                if (item is TextBox)
                {
                    var elem = (item as TextBox);
                    if (elem.Name.Equals("txtReason"))
                    {
                        continue;
                    }
                    (item as TextBox).IsEnabled = false; //批量设置输入框禁用
                }
                else if (item is ComboBox)
                {
                    (item as ComboBox).IsEnabled = false;
                }
            }

            #endregion

        }

        //驳回
        private void reject_Click(object sender, RoutedEventArgs e)
        {
            Teshu_gas_check_history info = new Teshu_gas_check_history();
            info.Teshu_gas_key = TeshuGas.Id;
            info.Check_stat = 0;
            info.Reject_reason = this.txtReason.Text;
            var result = teshuBusiness.Check(info);
            ShowAlertHelpter.ShowAlert(result.Msg);

            RefreshPageList?.Invoke(this, e);
            this.Close();
        }

        //退出
        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            RefreshPageList?.Invoke(this, e);
            this.Close();
        }

        //通过
        private void Pass_Click(object sender, RoutedEventArgs e)
        {
            Teshu_gas_check_history info = new Teshu_gas_check_history();
            info.Teshu_gas_key = TeshuGas.Id;
            info.Check_stat = 1;
            var result = teshuBusiness.Check(info);
            if (result.CodeEnum == ResultCode.Success)
            {
                //审核通过的，生成PDF文件 //创建时间：2023-1-12 10:07:39。作者：王浩力
                //读取文本实体
                var infoPr = teshuBusiness.GetView(TeshuGas.Id);

                //备注 ，标准。创建时间：2023-1-13 10:36:37
                var biaozhunList = sourceDictBusiness.GetListByGroup("teshu_report_remark", 1);
                biaozhunList = biaozhunList.OrderBy(g => g.Createtime).ToList();
                infoPr.source_Dicts = biaozhunList;

                string pdfFileName = $"特殊报告{TeshuGas.Report_no}井{TeshuGas.Well_no}";
                MakePdfHelpter.CheckPassLoadXps(pdfFileName, "FlowDocumentTeshu.xaml", infoPr, () =>
                {
                    string dir = DateTime.Now.ToString("yyyy");
                    dir = System.IO.Path.Combine("特殊报告", dir);
                    return dir;
                });
            }
            ShowAlertHelpter.ShowAlert(result.Msg);

            RefreshPageList?.Invoke(this, e);
            this.Close();
        }
    }
}
